package com.csun.dsms.controller;

import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsFile;
import com.csun.dsms.model.domain.DsmsFileResource;
import com.csun.dsms.model.dto.file.FileDto;
import com.csun.dsms.model.dto.file.FileRelevance;
import com.csun.dsms.service.DsmsFileResourceService;
import com.csun.dsms.service.DsmsFileService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private DsmsFileService fileService;

    @Resource
    private DsmsFileResourceService fileResourceService;

    @PostMapping(value = "/save")
    public Wrapper save(@RequestBody FileDto fileDto) {

        DsmsFile dsmsFile = new DsmsFile();
        dsmsFile.setReleasePeople(fileDto.getReleasePeople());
        dsmsFile.setTitle(fileDto.getTitle());
        dsmsFile.setStatus("1");
        dsmsFile.setUploadTime(new Date());
        fileService.save(dsmsFile);
        Long id = dsmsFile.getId();

        List<FileRelevance> fileRelevanceList = fileDto.getFileRelevanceList();
        if (!CollectionUtils.isEmpty(fileRelevanceList)) {
            for (FileRelevance fileRelevance : fileRelevanceList) {
                DsmsFileResource fileResource = new DsmsFileResource();
                fileResource.setFileId(id);
                fileResource.setResourceId(fileRelevance.getResourceId());
                fileResource.setResourcePath(fileRelevance.getResourcePath());
                fileResource.setFileName(this.getFileName(fileRelevance.getResourcePath()));
                fileResourceService.save(fileResource);
            }
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功");
    }

    @PostMapping(value = "/delete")
    public Wrapper delete(@RequestBody FileDto fileDto) {
        DsmsFile dsmsFile = new DsmsFile();
        dsmsFile.setId(fileDto.getId());
        dsmsFile.setStatus("99");
        fileService.update(dsmsFile);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功");
    }

    @PostMapping(value = "/download")
    public Wrapper download(@RequestBody FileDto fileDto) {
        Example example = new Example(DsmsFileResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("fileId", fileDto.getId());
        List<DsmsFileResource> dsmsFileResources = fileResourceService.selectByExample(example);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功", dsmsFileResources);
    }

    @PostMapping(value = "/list")
    public Wrapper list(@RequestBody FileDto fileDto) {
        PageHelper.startPage(fileDto.getPageNum(), fileDto.getPageSize());
        Example example = new Example(DsmsFile.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", "1");
        List<DsmsFile> dsmsFiles = fileService.selectByExample(example);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功", new PageInfo(dsmsFiles));
    }

    private String getFileName(String path) {
        if (null == path || "".equals(path)) {
            return "";
        }
        return path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
    }


}